package com.hy.mydesktop.client.mvc.desktop;

import java.util.Map;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.hy.mydesktop.client.MyDesktop;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.factory.GxtDesktopComponentFactory;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.util.ControllerUtil;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;


/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-9-14；时间：下午01:30:41</li>
 * <li>类型名称：ShortcutController</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class ShortcutController extends BaseController {
private ShortcutPresenter shortcutPresenter;
	
	
	public ShortcutController() {
		MyLoggerUtil.printControllerDebugForInit(getClass());

		
	    
	  }

	@Override
	public void handleEvent(AppEvent event) {
		
		//shortcutView.getShortcut()??
		/*EventStructureMetaModel eventStructureMetaModel = null;
		Object object =((Map<String, Object>)event.getData()).get("eventstructuremetamodel");
		eventStructureMetaModel = (EventStructureMetaModel)object;*/
		EventStructureMetaModel eventStructureMetaModel =(EventStructureMetaModel)((Map<String, Object>)event.getData()).get("eventstructuremetamodel");
		
		//0002
		if (AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER==event.getType()) {
			//通过EventStructureMetaModel识别，是否应该处理该事件
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
				forwardToView(shortcutPresenter, event);
				 this.forwardToChild(event);
			}
			 
		}; 
		//0003
		if (AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT==event.getType()) {
			//通过EventStructureMetaModel识别，是否应该处理该事件
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
				forwardToView(shortcutPresenter, event);
				 this.forwardToChild(event);
			}
		};
		if (AppEvents.DESKTOP_SHORTCUT_ONCLICK==event.getType()) {
			//通过EventStructureMetaModel识别，是否应该处理该事件
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
				forwardToView(shortcutPresenter, event);
				 this.forwardToChild(event);
			}
		};
		/**
		 * 将事件转发给子节点，及子控制器
		 * 注:当前控制器，含有子节点的时候，必不可少
		 */
		//this.forwardToChild(event);
		/*枚举每一个可能需要响应的事件，以及其需要的处理方式
		ifelse switch (key) {
			case value:
				
				break;

			default:
				break;
		}*/
	}
	
	public void initialize() {
		
		Desktop desktop = (Desktop)Registry.get(MyDesktop.DESKTOP);
		Shortcut shortcut = (Shortcut)GxtDesktopComponentFactory.addOneGxtComponentToDesktop( getGxtComponentMetaNodeModel());
		//MyLoggerUtil.printComponentDebugForInit(Shortcut.class);
		ShortcutView shortcutView = new ShortcutView(shortcut);
		shortcutPresenter = new ShortcutPresenter(this,shortcutView);
		
		initialized = true;
		 
	  }
	
	protected void onError(AppEvent ae) {
	    System.out.println("error: " + ae.<Object>getData());
	  }
}
